package com.ht.managermentserver.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.managermentserver.entity.SysUser;
import com.ht.managermentserver.mapper.SysUserMapper;
import com.ht.managermentserver.security.JwtUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户临时使用表 服务类
 *
 * @author czy
 * @since 2020-05-26
 */
@Service
@DS("master")
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser>
  implements UserDetailsService {
  @Override
  public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
    final QueryWrapper queryWrapper = new QueryWrapper();
    queryWrapper.eq(SysUser.USER_NAME, username);

    final List<SysUser> userList = this.list(queryWrapper);
    if (userList == null || userList.size() == 0) {

      return null;
    }
    final SysUser user = userList.get(0);
    return new JwtUser(user);
  }
}
